﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using VOL.Core.BaseProvider;
using VOL.Core.Extensions;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels;
using VOL.Entity.DomainModels.EquipmentManage;
using VOL.SmartMecial.Dtos;
using VOL.SmartMecial.Infrastructure;
using VOL.SmartMecial.IServices.EquipmentManage;

namespace VOL.SmartMecial.Services.EquipmentManage
{
    public class EquipmentRecordService : IEquipmentRecordService
    {
        private readonly IBaseRepository<EquipmentRecord> _equipmentRecordRepos;

        public EquipmentRecordService(IBaseRepository<EquipmentRecord> equipmentRecordRepos)
        {
            this._equipmentRecordRepos = equipmentRecordRepos;
        }

        /// <summary>
        /// 设备借还分页显示
        /// </summary>
        /// <param name="equipmentRecordDTO">查询条件</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">条数</param>
        /// <returns>返回</returns>
        public Task<WebResponseContent> GetEquipmentRecord(EquipmentRecordDTO equipmentRecordDTO, int pageIndex, int pageSize)
        {
            var equipmentRecords = _equipmentRecordRepos.GetAll();
            equipmentRecords = equipmentRecords.WhereIF(!string.IsNullOrEmpty(equipmentRecordDTO.EquipmentType), c => c.EquipmentType == equipmentRecordDTO.EquipmentType);
            equipmentRecords = equipmentRecords.WhereIF(!string.IsNullOrEmpty(equipmentRecordDTO.EquipmentNo),c => c.EquipmentNo == equipmentRecordDTO.EquipmentType);
            equipmentRecords = equipmentRecords.WhereIF(equipmentRecordDTO.State > 0, c => c.State == equipmentRecordDTO.State);
            equipmentRecords = equipmentRecords.WhereIF(equipmentRecordDTO.borrowDate != null, c => c.borrowDate > equipmentRecordDTO.borrowDate && equipmentRecordDTO.borrowDate > c.borrowDate);
            equipmentRecords = equipmentRecords.WhereIF(!string.IsNullOrEmpty(equipmentRecordDTO.Borrower), c => c.Borrower.Contains(equipmentRecordDTO.Borrower));

            equipmentRecords = equipmentRecords.WhereIF(!string.IsNullOrEmpty(equipmentRecordDTO.ReturnUser), c => c.ReturnUser.Contains(equipmentRecordDTO.ReturnUser));

            int count = equipmentRecords.Count();
            var equipmentRecordslist = equipmentRecords.OrderBy(c => c.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var pageGrid = new PageGridData<EquipmentRecord>();

            pageGrid.total = count;
            pageGrid.rows = equipmentRecordslist;
            var res = new WebResponseContent();
            res.OK("显示成功", pageGrid);
            return Task.FromResult(res);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="record">实体</param>
        /// <returns>返回</returns>
        public async Task<int> Add(EquipmentRecord record)
        {
            record.State = 1;
            var i = await _equipmentRecordRepos.AddAsync(record);
            return i;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="record">实体</param>
        /// <returns>返回</returns>
        public async Task<int> Update(EquipmentRecord record)
        {
            record.State = 2;
            var i  = await _equipmentRecordRepos.UpdateAsync(record); 
            return  i;
           
        }

        /// <summary>
        /// 同一个设备类型的设备编号不能被重复借出。
        /// </summary>
        /// <param name="equipmentNo">设备编号</param>
        /// <param name="equipmentType">设备类型</param>
        /// <returns>返回</returns>
       public async Task<WebResponseContent> CheckEquipmentStatus(string equipmentNo, string equipmentType)
        {
            var equipmentRecord = await _equipmentRecordRepos.GetAll().Where(m => m.EquipmentNo == equipmentNo && m.EquipmentType == equipmentType).ToListAsync();
            var res = new WebResponseContent();
            res.OK("查询成功", equipmentRecord);
            return await Task.FromResult(res);
        }

    }
}
